<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="品牌广告任务" :visible.sync="dialogVisible" @close="resetForm('addForm')" width="400px">
        <el-form label-position="top" :model="addForm" :hide-required-asterisk='true' ref="addForm" size="medium" :rules="addRules" class="dialog_form">
            <BrandModel :addForm="addForm" :addRules="addRules" />
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm('addForm')" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm('addForm')" :disabled="loading" :loading="loading" class="el-button-width">保 存</el-button>
        </span>
    </el-dialog>
</template>

<script>
import BrandModel from "./BrandModel";
export default {
    components: {
        BrandModel,
    },
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            addForm: {
                project_id: null,
                brandads_name: "",
                brandads_amount: "",
                upstream_ratio: "",
                spit_ratio: "",
                push_date: "",
            },
            addRules: {
                checkFloat: [
                    {
                        pattern:
                            /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
                        required: true,
                        message: "请输入正确数值",
                        trigger: ["blur", "change"],
                    },
                ],
            },
        };
    },
    computed: {},
    methods: {
        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.addForm.project_id = this.$route.query.project_id
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "fxdata/revenue/brandadsAdd",
                        this.addForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "保存成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.resetForm(formName);
                        this.$emit("getBrandList", 1);
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
};
</script>


<style scoped>
</style>
